package com.ruoyi.web.controller.books;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.books.domain.BookManage;
import com.ruoyi.books.service.IBookManageService;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.server.MQTT.listener.MqttHandler;
import com.ruoyi.system.service.ISysDictDataService;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.integration.mqtt.support.MqttHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.books.domain.BookRecord;
import com.ruoyi.books.service.IBookRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;


/**
 * 领还记录表单Controller
 *
 * @author wt
 * @date 2025-03-16
 */
@RestController
@RequestMapping("/books/record")
public class BookRecordController extends BaseController
{
    @Autowired
    private IBookRecordService bookRecordService;
    @Autowired
    private ISysDictDataService iSysDictDataService;
    @Autowired
    private IBookManageService iBookManageService;
    
    
    /**
     * 查询领还记录表单列表
     */
    @PreAuthorize("@ss.hasPermi('books:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(BookRecord bookRecord)
    {
        startPage();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        boolean admin = SecurityUtils.isAdmin(user.getUserId());
        if (!admin){
            bookRecord.setUserId(user.getUserId());
        }
        List<BookRecord> list = bookRecordService.selectBookRecordList(bookRecord);
        return getDataTable(list);
    }

    /**
     * 导出领还记录表单列表
     */
    @PreAuthorize("@ss.hasPermi('books:record:export')")
    @Log(title = "领还记录表单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BookRecord bookRecord)
    {
        List<BookRecord> list = bookRecordService.selectBookRecordList(bookRecord);
        ExcelUtil<BookRecord> util = new ExcelUtil<BookRecord>(BookRecord.class);
        util.exportExcel(response, list, "领还记录表单数据");
    }
    
    @Autowired
    MqttHandler mqttHandler;
    /**
     * 获取领还记录表单详细信息
     */
    @PreAuthorize("@ss.hasPermi('books:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) throws MqttException {
        mqttHandler.publish(MqttHandler.SON_TOPIC + "ce:6e:63:d3:34:32/" + MqttHandler.TOPIC_REQUEST,"子通道测试数据",MqttHandler.QOS_0);
        mqttHandler.publish(MqttHandler.MAIN_TOPIC +  MqttHandler.TOPIC_REQUEST,"主通道测试数据",MqttHandler.QOS_0);
        return success(bookRecordService.selectBookRecordById(id));
    }

    /**
     * 新增领还记录表单
     */
    @PreAuthorize("@ss.hasPermi('books:record:add')")
    @Log(title = "领还记录表单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BookRecord bookRecord)
    {
        //把书籍信息同步为借出
        BookManage bookManage = new BookManage();
        bookManage.setId(bookRecord.getBookId());
        bookManage.setState("1");
        iBookManageService.updateBookManage(bookManage);
        return toAjax(bookRecordService.insertBookRecord(bookRecord));
    }

    /**
     * 修改领还记录表单
     */
    @PreAuthorize("@ss.hasPermi('books:record:edit')")
    @Log(title = "领还记录表单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BookRecord bookRecord)
    {
        bookRecord.setInTime(new Date());
        //把书籍信息同步为借出
        BookManage bookManage = new BookManage();
        bookManage.setId(bookRecord.getBookId());
        bookManage.setState("0");
        iBookManageService.updateBookManage(bookManage);
        return toAjax(bookRecordService.updateBookRecord(bookRecord));
    }

    /**
     * 删除领还记录表单
     */
    @PreAuthorize("@ss.hasPermi('books:record:remove')")
    @Log(title = "领还记录表单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(bookRecordService.deleteBookRecordByIds(ids));
    }
    
    @PreAuthorize("@ss.hasPermi('books:record:list')")
    @GetMapping("/getBookTypes/{type}")
    public TableDataInfo getBookTypes(@PathVariable String type){
        SysDictData dictData = new SysDictData();
        dictData.setDictType(type);
        return getDataTable(iSysDictDataService.selectDictDataList(dictData));
    }
}
